<template>
	<view class="list">
		<view>
			<uni-search-bar placeholder="请输入关键字" :value="searchValue" cancelButton="none" @focus="focus" bgColor="#fff">
			</uni-search-bar>
			<LineBorder></LineBorder>
		</view>
		<view class="lists">
			<!-- 左侧滑动 -->
			<scroll-view scroll-y="true" class="list-left" :style="'height'+clentHeight+'px;'">
				<view class="left-item" v-for="(i,j) in leftData" :key="i.id" @click="clickLeft(j,i.id)">
					<view class="left-name " :class="currentIndex===j?'left-name-active':''">
						{{i.name}}
					</view>
				</view>
			</scroll-view>
			<!-- 右侧滑动 -->
			<scroll-view scroll-y="true" class="list-right" :style="'height'+clentHeight+'px;'">
				<view class="right-list" v-for="(item,index) in rightData" :key="index">
					<block v-for="(k,idx) in item" :key="idx">
						<view class="list-title">
							{{k.name}}
						</view>
						<view class="right-content">
							<view class="right-item" v-for="(j,idx) in k.list" :key="j.id" @click="clickItem(j.id)">
								<image :src="j.imgUrl" mode="aspectFit" class="item-img"></image>
								<view class="item-name">
									{{j.name}}
								</view>
							</view>
						</view>
					</block>
				</view>
			</scroll-view>
		</view>
		<Tabbar cureentPage='list'></Tabbar>
	</view>
</template>

<script>
	import LineBorder from '@/components/common/LineBorder.vue';
	import http from "@/common/api/reqeust.js";
	import Tabbar from '@/components/common/Tabbar.vue';
	import {
		mapState
	} from 'vuex';

	export default {
		data() {
			return {
				searchValue: '11',
				currentIndex: 0,
				clentHeight: 0,
				leftData: [],
				rightData: [],
			}
		},
		components: {
			LineBorder,
			Tabbar
		},
		mounted() {
			this.getData()
		},
		methods: {
			// 获取大分类数据
			getData(id) {
				// 点击左侧同一个数据
				if (id === (this.currentIndex + 1)) {
					return
				}
				http.request({
					url: "/api/category/list",
				}).then((res) => {
					console.log(res.results, 'rrrr');
					let leftData = [];
					let rightData = []
					res.results.forEach(v => {
						console.log(JSON.parse(v.data));
						leftData.push({
							id: v.id,
							name: v.name
						})
						if (v.id === (this.currentIndex + 1)) {
							rightData.push(JSON.parse(v.data))
						}
					})
					this.leftData = leftData
					this.rightData = rightData
				})
			},
			// 去搜索页
			focus(e) {
				uni.navigateTo({
					url: "/pages/search/search"
				})
			},
			clickLeft(index, id) {
				this.getData(id)
				this.currentIndex = index
			},
			clickItem() {
				uni.navigateTo({
					url: '/pages/searcList/searcList',
				});
			},
			/*获取可视区域高度*/
			getClentHeight() {
				const res = uni.getSystemInfoSync();
				const system = res.platform;
				if (system === 'ios') {
					return 44 + res.statusBarHeight
				} else if (system === 'android') {
					return 48 + res.statusBarHeight
				} else {
					return 0
				}
			}
		},
		// 获取可视高度
		onReady() {
			uni.getSystemInfo({
				success: (res) => {
					this.clentHeight = res.windowHeight - this.getClentHeight()
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.lists {
		display: flex;

		.list-left {
			width: 200rpx;

			.left-item {
				border-bottom: solid 2rpx #fff;
				font-size: 28rpx;
				background-color: #e9e9e9;

				.left-name {
					padding: 30rpx 6rpx;
					text-align: center;
				}

				.left-name-active {
					border-left: solid 8rpx #49b0fb;
					background-color: #fff;
				}
			}
		}

		.list-right {
			flex: 1;
			padding-left: 30rpx;

			.right-item {
				.right-name {}
			}

			.right-list {
				.list-title {
					font-weight: bold;
					padding: 20rpx 0;
				}

				.right-content {
					display: flex;
					flex-wrap: wrap;

					.right-item {
						width: 150rpx;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;
						padding: 10rpx;

						.item-img {
							width: 150rpx;
							height: 150rpx;
							object-fit: cover;
						}

						.item-name {
							padding: 16rpx 0;
						}
					}
				}
			}
		}
	}
</style>